﻿using System;
using System.Threading;
using Wicture.EPC.OnlineQueryProxy.Agents;
using Wicture.EPC.OnlineQueryProxy.Processors;

namespace Wicture.EPC.OnlineQueryProxy
{
    public class OnlineQueryContext : IDisposable
    {
        public OnlineQueryContext(IQueryProcessor processor, Agents.Agent agent, QueryRequest request)
        {
            Processor = processor;
            Agent = agent;
            Request = request;
            Result = new QueryResponse();
            AutoResetEvent = new AutoResetEvent(false);

            Agent.IsBusy = true;
            Request.AgentId = agent.Id;
        }

        public QueryRequest Request { get; set; }

        public QueryResponse Result { get; set; }

        public AutoResetEvent AutoResetEvent { get; set; }

        public Agent Agent { get; set; }

        public IQueryProcessor Processor { get; set; }

        public void Dispose()
        {
            AutoResetEvent.Reset();
            AutoResetEvent.Dispose();

            QueryContextManager.Clear(this);
        }
    }
}
